package swd.maintain.system.sync_db_table;

import java.sql.*;
import java.util.*;

import com.pub.database.*;
import swd.base.*;

public class Business extends SwdBase{
    QueryBean gqbnSrcDAO=null;
    QueryBean gqbnTrgDAO=null;
    public Business(QueryBean pqbnSrcDAO,QueryBean pqbnTrgDAO){
        super(null,null,pqbnSrcDAO);
        gqbnSrcDAO=pqbnSrcDAO;
        gqbnTrgDAO=pqbnTrgDAO;
   }
   /**
    * getData
    *
    * @param view View
    * @param pstrUserNO String
    * @return String
    */
   String getData(View view, String pstrUserNO) throws Exception {

       if(view.getDataKind()!=null && view.getDataKind().equals("getTables")){
           return getTableList();
       }

       return "";
    }

    String getTableList() throws Exception {
        ResultSet rs=null;
        StringBuffer lstbTableList = new StringBuffer();
        String lstrTableName=null;

        try{
            rs = gqbnSrcDAO.getConnection().getMetaData().getTables(null,null,null,new String[]{"TABLE"});

            while(rs.next()){
                lstrTableName = new String(rs.getString("TABLE_NAME"));
                lstbTableList.append(lstrTableName);
                lstbTableList.append("/");
                lstbTableList.append(lstrTableName);
                lstbTableList.append("/");
                lstbTableList.append("false,");
            }

            return lstbTableList.substring(0,lstbTableList.length()-1);
        }
        catch(Exception e){
            throw new Exception(e.fillInStackTrace());
        }
        finally{
          if(rs!=null){
            try{
              rs.close();
              rs=null;
            }
            catch(Exception se){
              se.printStackTrace();
            }
          }
        }
    }

    void setDB_alias_list(View view) throws Exception {
        StringBuffer lstbDBAlias = new StringBuffer();
        ArrayList<String> larlDBAliasList = null;

        try{
            larlDBAliasList = DBFunction.getAllDbAliasList();

            for(int i=0;i<larlDBAliasList.size();i++){
                lstbDBAlias.append("<option value='");
                lstbDBAlias.append(larlDBAliasList.get(i).toString());
                lstbDBAlias.append("'>");
                lstbDBAlias.append(larlDBAliasList.get(i).toString());
                lstbDBAlias.append("</option>");
            }

            view.setDb_alias_list(lstbDBAlias.toString());
        }
        catch(Exception e){
            throw new Exception(e.fillInStackTrace());
        }
        finally{
            if(larlDBAliasList!=null){
                larlDBAliasList.clear();
            }
        }
    }

    /**
     * transferDB
     *
     * @param view View
     */
    void transferDB(View view) throws Exception{
        ArrayList<String> larlTables = null;

        try{
            larlTables = new ArrayList<String>(Arrays.asList(view.getTransfer_table_list()));

            for(int i=0;i<larlTables.size();i++){
                if(!checkTargetDbTableExists(larlTables.get(i).toString()) && view.isCreateTable()){
                    createTableOnTargetDB(larlTables.get(i).toString());
                }
                else{
                    continue;
                }

                //transferTableData(view.getSrc_db_alias(),view.getTrg_db_alias(),larlTables.get(i).toString());
            }
        }
        catch(Exception e){
            if(view.getDBDebug()){
                e.printStackTrace();
            }
            throw new Exception(e.fillInStackTrace());
        }
        finally{
            if(larlTables!=null){
                larlTables.clear();
                larlTables=null;
            }
        }
    }

    /**
     * createTableOnTargetDB
     *
     * @param pstrDbf_no String
     */
    private void createTableOnTargetDB(String pstrDbf_no) {

    }

    /**
     * checkTableExists
     *
     * @param pstrTrg_db_alias String
     * @param pstrDb_no String
     * @return boolean
     */
    boolean checkTargetDbTableExists(String pstrDb_no) {
        return gqbnTrgDAO.readSingleValueData("select count(*) from " + pstrDb_no + " where 1=2")!=null;
    }

}
